﻿using System;
using System.ComponentModel.Composition;
using Csla.Xaml;
using Microsoft.Practices.Prism.Regions;
using BizControl.Library.BusinessObjects.Activities;
using BizControl.Presentation.Infrastructure.Helpers;
using Microsoft.Practices.Prism;
using BizControl.Presentation.Infrastructure.Constants;
using System.ComponentModel;
using BizControl.Presentation.Infrastructure.Controls;
using Microsoft.Practices.Prism.Commands;
using System.Windows.Input;
using Csla;
using BizControl.Library.Security;
using BizControl.Library.BusinessObjects.Marks;
using System.Collections.ObjectModel;

namespace BizControl.Presentation.ActivityEditModule.ViewModels
{
  public class MarksConditionsViewModel : ViewModel<ActivityMarks>, IHeaderInfoProvider<string>
  {
    private readonly DelegateCommand<ActivityMarkBO> _addActivityMarkConditionCommand;
    private readonly DelegateCommand<ActivityMarkBO> _addActivityConditionCommand;
    private readonly DelegateCommand<ActivityMarkBO> _addSkillConditionCommand;

    public MarksConditionsViewModel(ActivityBO activity)
    {
      ManageObjectLifetime = false;
      Activity = activity;
      _addActivityMarkConditionCommand = new DelegateCommand<ActivityMarkBO>(AddActivityMarkCondition);
      _addActivityConditionCommand = new DelegateCommand<ActivityMarkBO>(AddActivityCondition);
      _addSkillConditionCommand = new DelegateCommand<ActivityMarkBO>(AddSkillCondition);

    }

    private void Refresh()
    {
      DoRefresh(GetActivityMarks);
    }

    protected override void OnModelChanged(ActivityMarks oldValue, ActivityMarks newValue)
    {
      base.OnModelChanged(oldValue, newValue);
      RefreshAllProperties();
    }

    private void RefreshAllProperties()
    {
    }

    ActivityMarks GetActivityMarks()
    {
      if (Activity != null)
        return Activity.Marks;
      return null;
    }

    #region Handlers

    private void AddActivityMarkCondition(ActivityMarkBO mark)
    {
      mark.Conditions.AddMarkCondition(ConditionType.MarkRequired);
    }

    private void AddActivityCondition(ActivityMarkBO mark)
    {
      //mark.Conditions.AddMarkCondition(ConditionType.ActivityRequired);
    }

    private void AddSkillCondition(ActivityMarkBO mark)
    {
      mark.Conditions.AddMarkCondition(ConditionType.SkillRequired);
    }

    #endregion

    #region Properties

    public ICommand AddActivityMarkConditionCommand
    {
      get { return _addActivityMarkConditionCommand; }
    }

    public ICommand AddActivityConditionCommand
    {
      get { return _addActivityConditionCommand; }
    }

    public ICommand AddSkillConditionCommand
    {
      get { return _addSkillConditionCommand; }
    }

    private ActivityBO _activity;
    public ActivityBO Activity
    {
      get { return _activity; }
      set
      {
        if (_activity != value)
        {
          _activity = value;
          Refresh();
          OnPropertyChanged("Activity");
        }
      }
    }

    #endregion

    #region IHeaderInfoProvider<string> Members

    public string HeaderInfo
    {
      get { return BizControl.Presentation.ActivityEditModule.Properties.Resources.MarksConditionsHeaderInfo; }
    }

    #endregion
  }

}
